const express = require('express');
const router = express.Router();
const NoticeCategory = require('../mysql/collection/NoticeCategory');

/**
 * @api {get} /admin/notice-category/list 获取所有分类
 * @apiName GetNoticeCategoryList
 * @apiGroup NoticeCategory
 * @apiHeader {String} token  token
 * @apiParam {Number} [pageSize=10] 每页显示数量
 * @apiParam {Number} [pageNum=1] 当前页码
 * @apiSuccess {Object} data 分页数据
 * @apiSuccess {Object[]} data.list 分类列表
 * @apiSuccess {String} data.list.categoryId 分类ID
 * @apiSuccess {String} data.list.name 分类名称
 * @apiSuccess {Number} data.total 总记录数
 * @apiSuccess {Number} data.pageSize 每页显示数量
 * @apiSuccess {Number} data.pageNum 当前页码
 * @apiSuccessExample {json} Success-Response:
 *  {
 *    code: 200,
 *    data: {
 *      list: [
 *        {
 *          categoryId: '1001',
 *          name: '系统公告'
 *        }
 *      ],
 *      total: 1,
 *      pageSize: 10,
 *      pageNum: 1
 *    }
 *  }
 */
router.get('/list', async (req, res) => {
  try {
    const { pageSize = 10, pageNum = 1 } = req.query;
    const skip = (pageNum - 1) * pageSize;
    
    const [categories, total] = await Promise.all([
      NoticeCategory.find({ flag: true }).skip(skip).limit(parseInt(pageSize)),
      NoticeCategory.countDocuments({ flag: true })
    ]);
    
    res.json({ 
      code: 200, 
      data: {
        list: categories,
        total,
        pageSize: parseInt(pageSize),
        pageNum: parseInt(pageNum)
      }
    });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

/**
 * @api {post} /admin/notice-category/create 创建分类
 * @apiName CreateNoticeCategory
 * @apiGroup NoticeCategory
 * @apiHeader {String} token  token
 * @apiParam {String} name 分类名称
 * @apiSuccess {Object} data 创建的分类
 * @apiSuccess {String} data.categoryId 分类ID
 * @apiSuccess {String} data.name 分类名称
 * @apiSuccessExample {json} Success-Response:
 *  {
 *    code: 200,
 *    data: {
 *      categoryId: '1001',
 *      name: '系统公告'
 *    }
 *  }
 */
router.post('/create', async (req, res) => {
  try {
    const { name } = req.body;
    if (!name) {
      return res.status(400).json({ code: 400, message: '分类名称不能为空' });
    }
    
    const category = new NoticeCategory({ 
      categoryId: Date.now().toString(),
      name 
    });
    
    await category.save();
    res.json({ code: 200, data: category });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

/**
 * @api {put} /admin/notice-category/update/:id 更新分类
 * @apiName UpdateNoticeCategory
 * @apiGroup NoticeCategory
 * @apiHeader {String} token  token
 * @apiParam {String} id 分类ID
 * @apiParam {String} name 分类名称
 * @apiSuccess {Object} data 更新的分类
 * @apiSuccess {String} data.categoryId 分类ID
 * @apiSuccess {String} data.name 分类名称
 * @apiSuccessExample {json} Success-Response:
 *  {
 *    code: 200,
 *    data: {
 *      categoryId: '1001',
 *      name: '更新后的分类名称'
 *    }
 *  }
 */
router.put('/update/:id', async (req, res) => {
  try {
    const { name } = req.body;
    if (!name) {
      return res.status(400).json({ code: 400, message: '分类名称不能为空' });
    }
    
    const category = await NoticeCategory.findByIdAndUpdate(
      req.params.id,
      { name, updateTime: Date.now() },
      { new: true }
    );
    
    if (!category) {
      return res.status(404).json({ code: 404, message: '分类不存在' });
    }
    
    res.json({ code: 200, data: category });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

/**
 * @api {delete} /admin/notice-category/delete/:id 删除分类
 * @apiName DeleteNoticeCategory
 * @apiGroup NoticeCategory
 * @apiHeader {String} token  token
 * @apiParam {String} id 分类ID
 * @apiSuccess {Object} data 删除的分类
 * @apiSuccess {String} data.categoryId 分类ID
 * @apiSuccess {Boolean} data.flag 删除标记
 * @apiSuccessExample {json} Success-Response:
 *  {
 *    code: 200,
 *    data: {
 *      categoryId: '1001',
 *      flag: false
 *    }
 *  }
 */
router.delete('/delete/:id', async (req, res) => {
  try {
    const category = await NoticeCategory.findByIdAndUpdate(
      req.params.id,
      { flag: false, updateTime: Date.now() },
      { new: true }
    );
    
    if (!category) {
      return res.status(404).json({ code: 404, message: '分类不存在' });
    }
    
    res.json({ code: 200, data: category });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

module.exports = router;